package com.yunc.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.utils.R;
import com.yunc.modules.shop.entity.ShopOrderItem;
import com.yunc.modules.shop.service.IShopOrderItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单商品
 *
 * @author congcong tao
 * @date 2018/12/24 9:34
 */
@RestController
@RequestMapping("orderItem")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopOrderItemController {

    private final IShopOrderItemService shopOrderItemService;

    @GetMapping("/page")
    public R page(Page<ShopOrderItem> page, ShopOrderItem orderItem) {
        shopOrderItemService.page(page, new QueryWrapper<>(orderItem));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(ShopOrderItem orderItem) {
        QueryWrapper<ShopOrderItem> ew = new QueryWrapper<>();
        List<ShopOrderItem> list = shopOrderItemService.list(ew);
        return R.ok().put("list", list).put("orderItem", orderItem);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id) {
        ShopOrderItem orderItem = shopOrderItemService.getById(id);
        return R.ok().put("orderItem", orderItem);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(ShopOrderItem orderItem) {
        boolean flag = shopOrderItemService.save(orderItem);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(ShopOrderItem orderItem) {
        boolean flag = shopOrderItemService.updateById(orderItem);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
